﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayController : MonoBehaviour
{
    private Rigidbody rb;
    private GameObject focalPoint;

    public float speed;
    public bool isPowerUp;
    public GameObject ringSign;

    public float powerStrength;
    public float waitTime = 7.0f;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();

        focalPoint = GameObject.Find("Focal Point");
    }

    // Update is called once per frame
    void Update()
    {
        float forwardInput = Input.GetAxis("Vertical");

        rb.AddForce(focalPoint.transform.forward * speed * forwardInput);

        ringSign.SetActive(isPowerUp);

        if(isPowerUp)
        {
            ringSign.transform.position = transform.position + new Vector3(0, -0.5f, 0);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("PowerUp"))
        {
            isPowerUp = true;
            Destroy(other.gameObject);

            StartCoroutine(PowerUpCountDown());
        }
    }

    IEnumerator PowerUpCountDown()
    {
        yield return new WaitForSeconds(waitTime);
        isPowerUp = false;
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.CompareTag("Enemy") && isPowerUp)
        {
            Rigidbody enemyRb = collision.gameObject.GetComponent<Rigidbody>();

            Vector3 dirFromPlayer = collision.transform.position - transform.position;

            enemyRb.AddForce(dirFromPlayer * powerStrength, ForceMode.Impulse);
        }
    }
}
